package com.viphuli.adapter;

import java.util.List;

import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import butterknife.Bind;
import butterknife.ButterKnife;

import com.offroader.utils.ViewUtils;
import com.viphuli.app.R;
import com.viphuli.common.Constants;
import com.viphuli.fragment.BaseProgressFragment;
import com.viphuli.http.bean.part.PurchasePayTicket;

public class TicketAdapter extends BaseAdapter {
	private BaseProgressFragment frag;
	private List<PurchasePayTicket> list;

	public TicketAdapter(BaseProgressFragment frag, List<PurchasePayTicket> list) {
		this.frag = frag;
		this.list = list;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int index) {
		return list.get(index);
	}

	@Override
	public long getItemId(int index) {
		return index;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder holder = null;

		if (convertView == null || convertView.getTag() == null) {
			convertView = ViewUtils.inflateView(parent.getContext(), R.layout.listview_ticket_item);
			holder = new ViewHolder(convertView);
			convertView.setTag(holder);

		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		//填充数据
		PurchasePayTicket ticket = list.get(position);

		holder.text.setText(ticket.getCoverShown());
		holder.name.setText(ticket.getName());
		holder.time.setText(ticket.expireTimeDao());
		
		if(ticket.getIsValid() == Constants.TICKET_IS_VALID){
			
			holder.bg.setBackgroundResource(R.drawable.bg_ticket_is_available);
			holder.mark.setVisibility(View.GONE);
			
		}else{
			
			//根据状态设置
			switch (ticket.getStatus()) {
			case Constants.TICKET_STATUS_IS_AVAILABLE:

				holder.bg.setBackgroundResource(R.drawable.bg_ticket_is_available);
				holder.mark.setVisibility(View.GONE);

				break;

			case Constants.TICKET_STATUS_IS_UN_AVAILABLE:

				holder.bg.setBackgroundResource(R.drawable.bg_ticket_is_un_available);
				holder.mark.setImageResource(R.drawable.ic_ticket_is_un_available);
				holder.mark.setVisibility(View.VISIBLE);

				break;
				
			case Constants.TICKET_STATUS_IS_EXPIRES:

				holder.bg.setBackgroundResource(R.drawable.bg_ticket_is_un_available);
				holder.mark.setImageResource(R.drawable.ic_ticket_expires);
				holder.mark.setVisibility(View.VISIBLE);

				break;
				
			case Constants.TICKET_STATUS_IS_LOCK:

				holder.bg.setBackgroundResource(R.drawable.bg_ticket_is_un_available);
				holder.mark.setImageResource(R.drawable.ic_ticket_is_lock);
				holder.mark.setVisibility(View.VISIBLE);

				break;
				
			case Constants.TICKET_STATUS_IS_USE:

				holder.bg.setBackgroundResource(R.drawable.bg_ticket_is_un_available);
				holder.mark.setImageResource(R.drawable.ic_ticket_is_use);
				holder.mark.setVisibility(View.VISIBLE);

				break;

			default:
				break;
			}
			
		}
		
		

		//按钮绑定事件

		return convertView;
	}

	protected class ViewHolder {
		@Bind(R.id.id_account_ticket_item_bg)
		ViewGroup bg;
		@Bind(R.id.id_account_ticket_item_text)
		TextView text;
		@Bind(R.id.id_account_ticket_item_mark)
		ImageView mark;
		@Bind(R.id.id_account_ticket_item_name)
		TextView name;
		@Bind(R.id.id_account_ticket_item_time)
		TextView time;

		public ViewHolder(View view) {
			ButterKnife.bind(this, view);
		}
	}

}
